package collect;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author du.senlin
 * @Title:
 * @Description: 1.8前是怎么排序的
 * @date 2020/3/30 11:16
 */
public class SortBefore8 {
    /**
     * 1.8前需要实现比较器，看着费劲
     * @param args
     */
    public static void main(String[] args) {
        List<String> names = Arrays.asList("a", "b", "d", "c");
        Collections.sort(names, new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                return b.compareTo(a);
            }
        });

        Subject subject1 = new Subject("语文", "91.0","2");
        Subject subject2 = new Subject("语文", "91.0","3");
        Subject subject3 = new Subject("语文", "91.0","4");
        Subject subject4 = new Subject("语文", "91.0","1");
        List<Subject> subjectList = new ArrayList<>();
        subjectList.add(subject1);
        subjectList.add(subject2);
        subjectList.add(subject3);
        subjectList.add(subject4);
        List<Subject> subjectListNew = new ArrayList<>();
//        System.out.println(subjectList.toString());
        //排序生效，使用stream和collect
//        subjectListNew = subjectList.stream().sorted((s1, s2) -> s1.getGrade().compareTo(s2.getGrade())).collect(Collectors.toList());
//        System.out.println(subjectList.toString());
        //[{"grade":90.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":94.0,"name":"语文"}]
//        System.out.println(subjectListNew.toString());//原集合没变
        //[{"grade":91,"id":1,"name":"语文"},{"grade":94,"id":2,"name":"数学"},{"grade":92,"id":3,"name":"英语"},{"grade":90,"id":4,"name":"语文"}]

        //排序生效，不使用stream和collect
//        subjectList.sort((s1, s2) -> s1.getGrade().compareTo(s2.getGrade()));
//        System.out.println(subjectListNew.toString());//原集合变化
        //[{"grade":90.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":94.0,"name":"语文"}]

        //排序不生效，使用stream时需要在末尾collect
//        subjectList.stream().sorted((s1, s2) -> s1.getGrade().compareTo(s2.getGrade()));
//        System.out.println(subjectListNew.toString());
        //[{"grade":91.0,"name":"语文"},{"grade":94.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":90.0,"name":"语文"}]

        //分数升序排列
//        subjectListNew = subjectList.stream().sorted((s1, s2) -> {
//            return s1.getGrade().compareTo(s2.getGrade());
//        }).collect(Collectors.toList());//如果使用分号，需要加{}和return
//        System.out.println(subjectListNew.toString());
        //[{"grade":90.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":94.0,"name":"语文"}]

        //分数降序排列
        subjectListNew = subjectList.stream().sorted(Comparator.comparing(Subject::getGrade).reversed().thenComparing(Subject::getAge)).collect(Collectors.toList());
        System.out.println(subjectListNew.toString());
        //[{"grade":94.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":90.0,"name":"语文"}]

        //分数升序排列
//        subjectListNew = subjectList.stream().sorted(Comparator.comparingDouble(Subject::getGrade)).collect(Collectors.toList());
//        System.out.println(subjectListNew.toString());
        //[{"grade":90.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":94.0,"name":"语文"}]


    }
}
